<?php

class Admin_ApiController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
        $adminValidation = new Validations_User();
        $valid = $adminValidation->hasidentity();
        if ($valid) {
            if ($adminValidation->getUserTypeId() != 1) {
                $this->view->message = "You are not admin.";
                Zend_Auth::getInstance()->clearIdentity();
                $this->_redirect('/admin/login');
            }
        } else {
            $this->_redirect('/admin/login');
        }
        $this->_helper->layout->setLayout('admin');
    }

    public function indexAction() {
        $api = new Admin_Model_Api();
        $this->view->allApi = $api->fetchAll();
    }

    public function addAction() {
        $form = new Admin_Form_Apiadd();
        $this->view->addForm = $form;

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                print_r($values);
                $db = new Admin_Model_Api();
                $db->addNewApi(array(
                    'name' => $values['name'],
                    'server_path' => $values['server_path'],
                    'api_key' => $values['api_key'],
                    'api_secret' => $values['api_secret']
                ));
                $this->_redirect('/admin/api');
            }
        }
    }

    public function sendAction() {

        $db = new Admin_Model_Api();
        $sendGridValues = $db->getApiByName('sendgrid');
        $values = $sendGridValues[0];
        $fromEmail = 'sjk.frills2008@gmail.com';
        $fromName = 'Savan Koradia @ Rent';
        $toEmail = 'savankumar_koradia@yahoo.com';
        $toName = 'Savan';
        $subject = 'SendGrid Testing';
        $body = 'This is the text of the mail using Zend and SendGrid mail.';

        /**
         * Check is sendgrid is active or send from local server.
         */
        $mail = new Zend_Mail();
        $mail->setFrom($fromEmail, $fromName);
        $mail->addTo($toEmail, $toName);
        $mail->setSubject($subject);

        if ($values['active'] == true) {
            $mailServerPath = $values['server_path'];
            $username = $values['api_key'];
            $password = $values['api_secret'];
            $config = array('ssl' => 'tls',
                'port' => '587',
                'auth' => 'login',
                'username' => $username,
                'password' => $password);
            $transport = new Zend_Mail_Transport_Smtp($mailServerPath, $config);
            $mail->setBodyText('This is by sendgrid.');
            $mail->send($transport);
        } else {//send from local server            
            $mail->setBodyText('This is by local server.');
            $mail->send();
        }


//        $config = array('ssl'=>'tls',
//            'port'=> '587',
//            'auth'=>'login',
//            'username'=>'sjk.frills2008',
//            'password'=>'c0deBurner');
//        $transport = new Zend_Mail_Transport_Smtp('smtp.sendgrid.net',$config);
//        $mail = new Zend_Mail();
//        $mail->setFrom('koradia.savan@gmail.com','Savan Koradia');
//        $mail->addTo('sjk.frills2008@gmail.com','Savan');
//        $mail->setSubject('Subject');
//        $mail->setBodyText('This is the text of the mail using Zend.');
//        $mail->send($transport);
    }

    public function updateAction() {
        $form = new Admin_Form_Apiupdate();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $apiId = $values['sr'];
                $db = new Admin_Model_Api();
                $db->updateApi($values, $apiId);
                $this->_redirect('/admin/api');
            }
        } else {
            // set filters and validators for GET input
            $filters = array(
                'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
            );
            $validators = array(
                'id' => array('Int')
            );
            $input = new Zend_Filter_Input($filters, $validators);
            $input->setData($this->getRequest()->getParams());


            //get value from database       
            $db = new Admin_Model_Api();
            $result = $db->getApiById($input->id);
            $form->mainForm($result);
        }
        $this->view->updateForm = $form;
    }
    
    public function activateapiAction(){
         $apiId = $this->getinputidAction();
         $db = new Admin_Model_Api();
         $data1 = $db->getApiById($apiId);
         $active = true;
         if($data1[0]['active']==true){
             $active = false;
         }else{
             $active = true;
         }
         $data = array('active'=>$active);
         $db->updateApi($data, $apiId);
         $this->_redirect('/admin/settings');
    }                
    
    
    /**
     * Get User ID.
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; //api id
        return $id;
    }
    
    
    
    

}

